<?php

class Admin_CapacityController extends Zend_Controller_Action {

    public function init() {
        
    }

    public function preDispatch() {
        // Autenticacion del usuario
        $auth = Zend_Auth::getInstance();
        if (!$auth->hasIdentity()) {
            $this->_helper->layout->disableLayout();
            $this->_forward("index", "index", "");
        } else {
            $identity = $auth->getIdentity();
            if ($identity['tipo_usuario'] != VirtualLabs_Admin_Usuario::ROL_ADMIN) {
                $this->_helper->layout->disableLayout();
                $this->_redirect('/');
            }
        }
        //fin de la autenicacion del usuario
    }

    public function indexAction() {
        
    }

    public function editAction() {
        $codigo = $this->getRequest()->getParam("id");
        //$debug = Zend_Registry::get(VirtualLabs_Common::DEBUG_LOG);        
        if (!isset($codigo)) {
            $this->view->codigoCapacity = "new";
        } else {
            $this->view->codigoCapacity = $codigo;
        }
    }

    public function getcapacityAction() {
        $this->_helper->viewRenderer->setNoRender(true);
        $this->_helper->layout->disableLayout();
        try {
            $capacityDao = new VirtualLabs_Admin_Capacity();
            $capacityList = $capacityDao->getAllConfigCapacity();
//        $debug = Zend_Registry::get(VirtualLabs_Common::DEBUG_LOG);
//        $debug->debug("capacity: " . print_r($list,TRUE));       
            $response = array();
            foreach ($capacityList as $capacity) {
                $response [] = array("<input type='image' src='/images/delete.png' onclick='deleteConfig({$capacity['codigo_configuracion_encendido_global']});' title='Eliminar Configuracion de Capacidad' />",
                    "<a href='#' onclick='loadConfig({$capacity['codigo_configuracion_encendido_global']});' title='Modificar la configuracion de capacidad'>{$this->mapDia($capacity['dia'] - 1)}</a>",
                    $capacity['hora_inicio'], $capacity['hora_fin'], $capacity['cantidad_cpu'] . '%', $capacity['cantidad_ram'] . '%');
                //, $this->mapEstado($capacity['estado']));
            }
            $aaData['aaData'] = $response;
            echo Zend_Json_Encoder::encode($aaData);
        } catch (Exception $exc) {
            $debug = Zend_Registry::get(VirtualLabs_Common::DEBUG_LOG);
            $debug->debug($exc->getMessage());
            $debug->debug($exc->getTraceAsString());
            echo '';
        }
    }

    /*
     * 
     */

    public function getcapacitybyidAction() {
        $this->_helper->viewRenderer->setNoRender(true);
        $this->_helper->layout->disableLayout();
        $codigo = $this->getRequest()->getParam("id");

        $response = array();
        try {
            $capacityDao = new VirtualLabs_Admin_Capacity();
            $capacity = $capacityDao->getConfigCapacityByCodigo($codigo);
            if (is_array($capacity)) {
                $response['response'] = 'ok';
                $response['data'] = $capacity;
            } else {
                $response['response'] = 'error';
                $response['message'] = 'Error: No se recuperaron registros';
            }
            echo Zend_Json_Encoder::encode($response);
        } catch (Exception $exc) {
            $debug = Zend_Registry::get(VirtualLabs_Common::DEBUG_LOG);
            $debug->debug($exc->getMessage());
            $debug->debug($exc->getTraceAsString());
            $response['response'] = 'error';
            $response['message'] = $exc->getMessage();
            echo Zend_Json_Encoder::encode($response);
        }
    }

    /*
     * 
     */

    private function mapDia($codigoDia) {
        $dias = array('Lunes', 'Martes', 'Mi&eacute;rcoles', 'Jueves', 'Viernes', 'S&aacute;bado', 'Domingo');
        return $dias[$codigoDia];
    }

    /*
     * 
     */

//    private function mapEstado($codigoEstado) {
//        $estados = array('Inactivo', 'Activo');
//        return $estados[$codigoEstado];
//    }

    public function savecapacityAction() {
        $this->_helper->viewRenderer->setNoRender(true);
        $this->_helper->layout->disableLayout();
        $codigo = $this->getRequest()->getParam("id");
        $hora_ini = $this->getRequest()->getParam("horaini");
        $hora_fin = $this->getRequest()->getParam("horafin");
        $dia = $this->getRequest()->getParam("dia");
        $cpu = $this->getRequest()->getParam("cpu");
        $mem = $this->getRequest()->getParam("memoria");
        $response = array();
        try {
            $capacityDao = new VirtualLabs_Admin_Capacity();
            $capacity = $capacityDao->validateHours($hora_ini, $hora_fin, $dia, $codigo);
            if ($capacity) {
                //Es posible guardar nueva configuracion o modificar la existente
                $capacityDao->savecapacity($codigo, $hora_ini, $hora_fin, $dia, $cpu, $mem);
                $response['response'] = 'ok';
            } else {
                $response['response'] = 'error';
                $response['message'] = 'No se puede guardar configuraciones con solapamiento de horas. Favor verificar las horas de inicio y fin';
            }
            echo Zend_Json_Encoder::encode($response);
        } catch (Exception $exc) {
            $debug = Zend_Registry::get(VirtualLabs_Common::DEBUG_LOG);
            $debug->debug($exc->getMessage());
            $debug->debug($exc->getTraceAsString());
            $response['response'] = 'error';
            $response['message'] = $exc->getMessage();
            echo Zend_Json_Encoder::encode($response);
        }
    }

    public function deletecapacityAction() {
        $this->_helper->viewRenderer->setNoRender(true);
        $this->_helper->layout->disableLayout();
        $codigo = $this->getRequest()->getParam("id");
        $response = array();
        try {
            $capacityDao = new VirtualLabs_Admin_Capacity();
            $capacityDao->deletecapacityById($codigo);
            $response['response'] = 'ok';
            echo Zend_Json_Encoder::encode($response);
        } catch (Exception $exc) {
            $debug = Zend_Registry::get(VirtualLabs_Common::DEBUG_LOG);
            $debug->debug($exc->getMessage());
            $debug->debug($exc->getTraceAsString());
            $response['response'] = 'error';
            $response['message'] = $exc->getMessage();
            echo Zend_Json_Encoder::encode($response);
        }
    }

}